package com.giveu.signature.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

import com.giveu.common.utils.FileUtil;
import com.giveu.signature.common.constants.FileSuffix;

public class SignFileUtil extends FileUtil {
	
	
	public static File genTemporaryFile(MultipartFile file) {
		String cd = System.getProperty("java.io.tmpdir");
        String tempName = UUID.randomUUID() + getSuffix(file);
		File dir = new File(cd);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File tempFile = new File(cd + File.separator + tempName);
        FileOutputStream fout = null;
        BufferedInputStream bin = null;
        try {
        	fout = new FileOutputStream(tempFile);
            bin = new BufferedInputStream(file.getInputStream());
            byte[] buffer = new byte[1024 * 10];
            int len = 0;
            while ((len = bin.read(buffer)) != -1) {
                fout.write(buffer, 0, len);
            }
            fout.flush();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally {
			try {
				fout.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				bin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return tempFile;
	}
	
	public static void main(String[] args) {
		
	}

	public static String readContent(File fl) throws Exception {
		String suffix = getSuffix(fl);
		if(FileSuffix.HTML.getSuffixList().contains(suffix)){
			return readStringContent(fl.getAbsolutePath());
		}else if(FileSuffix.DOC.getSuffixList().contains(suffix)){
			return DocToHtmlUtil.wordToHtml(fl.getAbsolutePath());
		}else if(FileSuffix.PDF.getSuffixList().contains(suffix)){
			
		}
		return null;
	}

	public static String getSuffix(MultipartFile file) {
		return file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
	}
	
	public static String getSuffix(File file) {
		return file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("."));
	}

	public static File genHtml(String htmlText) throws IOException {
		String cd = System.getProperty("java.io.tmpdir");
        String tempName = UUID.randomUUID() + ".html";
		File dir = new File(cd);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File tempFile = new File(cd + File.separator + tempName);
        if (!tempFile.exists()) {
        	tempFile.createNewFile();
        }
        writeToFile("<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Insert title here</title></head><body>", cd + File.separator + tempName, true, true);
		writeToFile(htmlText, cd + File.separator + tempName, true, true);
		writeToFile("</body></html>", cd + File.separator + tempName, true, true);
		return tempFile;
	}

}
